﻿using System.Windows;

using Microsoft.Practices.Composite.Events;
using Microsoft.Practices.Composite.Modularity;
using Microsoft.Practices.Composite.Presentation.Events;
using Microsoft.Practices.Composite.Regions;
using Microsoft.Practices.Unity;

using Matchingo.Common.Constants;
using Matchingo.Common.Types.Events;

namespace Matchingo.Modules.FlyoutEffectsModule
{
    public class FlyoutEffectsViewModel : DependencyObject, IModule
    {
        #region ========== Private Properties ==========

        private IFlyoutEffectsView View;


        private readonly IUnityContainer Container;
        private readonly IRegionManager RegionManager;
        private readonly IEventAggregator EventAggregator;

        #endregion

        #region ========== Constructor ==========

        public FlyoutEffectsViewModel(IUnityContainer container, IRegionManager regionManager, IEventAggregator eventAggregator)
        {
            Container = container;
            RegionManager = regionManager;
            EventAggregator = eventAggregator;
        }

        #endregion

        #region ========== Public Methods ==========

        public void Initialize()
        {
            View = Container.Resolve<IFlyoutEffectsView>();
            View.Initialize(this);

            IRegion region = RegionManager.Regions[CommonConstants.SHELL_REGION_Flyouts]; 
            region.Add(View);

            EventAggregator.GetEvent<ComboOccurredEvent>().Subscribe(ComboOccurredEventHandler, ThreadOption.UIThread);
        }

        #endregion

        #region ========== Event Handlers ==========

        // NOTE:: event handlers used by the EventAggregator need to be public in Silverlight
        public void ComboOccurredEventHandler(int comboCount)
        {
            if (comboCount < 2)
                return;

            View.FireComboFlyout(comboCount);
        }

        #endregion
    }
}
